home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 1 of 3 / CHAPTER9 / CRTCURSR.C < prev    next >
C/C++ Source or Header  |  1996-01-28  |  8KB  |  252 lines

  1.  
  2. #include <windows.h>  
  3. #include "CrtCursr.h" 
  4.  
  5.  
  6. #if defined (WIN32)
  7.     #define IS_WIN32 TRUE
  8. #else
  9.     #define IS_WIN32 FALSE
  10. #endif
  11.  
  12. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  13. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  14. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  15.  
  16. HINSTANCE hInst;   // current instance
  17.  
  18. LPCTSTR lpszAppName  = "MyApp";
  19. LPCTSTR lpszTitle    = "My Application"; 
  20.  
  21. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  22.  
  23. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  24.                       LPTSTR lpCmdLine, int nCmdShow)
  25. {
  26.    MSG      msg;
  27.    HWND     hWnd; 
  28.    WNDCLASS wc;
  29.  
  30.    // Register the main application window class.
  31.    //............................................
  32.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  33.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  34.    wc.cbClsExtra    = 0;                      
  35.    wc.cbWndExtra    = 0;                      
  36.    wc.hInstance     = hInstance;              
  37.    wc.hIcon         = LoadIcon( hInstance, lpszAppName ); 
  38.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  39.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  40.    wc.lpszMenuName  = lpszAppName;              
  41.    wc.lpszClassName = lpszAppName;              
  42.  
  43.    if ( IS_WIN95 )
  44.    {
  45.       if ( !RegisterWin95( &wc ) )
  46.          return( FALSE );
  47.    }
  48.    else if ( !RegisterClass( &wc ) )
  49.       return( FALSE );
  50.  
  51.    hInst = hInstance; 
  52.  
  53.    // Create the main application window.
  54.    //....................................
  55.    hWnd = CreateWindow( lpszAppName, 
  56.                         lpszTitle,    
  57.                         WS_OVERLAPPEDWINDOW, 
  58.                         CW_USEDEFAULT, 0, 
  59.                         CW_USEDEFAULT, 0,  
  60.                         NULL,              
  61.                         NULL,              
  62.                         hInstance,         
  63.                         NULL               
  64.                       );
  65.  
  66.    if ( !hWnd ) 
  67.       return( FALSE );
  68.  
  69.    ShowWindow( hWnd, nCmdShow ); 
  70.    UpdateWindow( hWnd );         
  71.  
  72.    while( GetMessage( &msg, NULL, 0, 0) )   
  73.    {
  74.       TranslateMessage( &msg ); 
  75.       DispatchMessage( &msg );  
  76.    }
  77.  
  78.    return( msg.wParam ); 
  79. }
  80.  
  81.  
  82. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  83. {
  84.    WNDCLASSEX wcex;
  85.  
  86.    wcex.style         = lpwc->style;
  87.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  88.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  89.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  90.    wcex.hInstance     = lpwc->hInstance;
  91.    wcex.hIcon         = lpwc->hIcon;
  92.    wcex.hCursor       = lpwc->hCursor;
  93.    wcex.hbrBackground = lpwc->hbrBackground;
  94.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  95.    wcex.lpszClassName = lpwc->lpszClassName;
  96.  
  97.    // Added elements for Windows 95.
  98.    //...............................
  99.    wcex.cbSize = sizeof(WNDCLASSEX);
  100.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  101.                             IMAGE_ICON, 16, 16,
  102.                             LR_DEFAULTCOLOR );
  103.             
  104.    return RegisterClassEx( &wcex );
  105. }
  106.  
  107. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  108. {
  109.    static HCURSOR  hCursor = NULL;
  110.    static int      nCursX, nCursY, nByteArea;
  111.    static HBITMAP  hBM;
  112.    static HDC      hDCBitmap;
  113.    static HANDLE   hmemAND, hmemXOR;
  114.  
  115.  
  116.    switch( uMsg )
  117.    {
  118.       case WM_CREATE:
  119.            {
  120.               HDC    hDC;
  121.               LPTSTR lpAND, lpXOR;
  122.  
  123.               // Get the cursor size.
  124.               //.....................
  125.               nCursX = GetSystemMetrics( SM_CXCURSOR ); 
  126.               nCursY = GetSystemMetrics( SM_CYCURSOR );
  127.  
  128.               // Create a memory DC and select a bitmap into it.
  129.               //................................................   
  130.               hBM = CreateBitmap( nCursX, nCursY, 1, 1, NULL );
  131.               hDC = GetDC( hWnd );
  132.               hDCBitmap = CreateCompatibleDC( hDC );
  133.               SelectObject( hDCBitmap, hBM );
  134.               ReleaseDC( hWnd, hDC );
  135.  
  136.               // Allocate memory for cursor masks.
  137.               //..................................
  138.               nByteArea = (nCursX/8) * nCursY;
  139.               hmemAND = GlobalAlloc( GMEM_MOVEABLE, (DWORD)nByteArea );
  140.               hmemXOR = GlobalAlloc( GMEM_MOVEABLE, (DWORD)nByteArea );
  141.  
  142.               // Lock memory for cursor masks.
  143.               //..............................
  144.               lpAND = GlobalLock( hmemAND );
  145.               lpXOR = GlobalLock( hmemXOR );
  146.  
  147.               // Create a gray rectangle cursor.
  148.               //................................
  149.               SelectObject( hDCBitmap, GetStockObject( LTGRAY_BRUSH ) );
  150.               PatBlt( hDCBitmap, 0, 0, nCursX, nCursY, PATCOPY );
  151.               GetBitmapBits( hBM, (DWORD)nByteArea, lpAND );
  152.  
  153.               // Set XOR memory to all 0's.
  154.               //.......................
  155.               memset( lpXOR, 0, nByteArea );
  156.  
  157.               hCursor = CreateCursor( hInst, 0, 0, nCursX, 
  158.                                       nCursY, lpAND, lpXOR );
  159.  
  160.               GlobalUnlock( hmemAND );
  161.               GlobalUnlock( hmemXOR );
  162.            }
  163.            break;
  164.  
  165.       case WM_SETCURSOR :
  166.               if ( hCursor )
  167.                  SetCursor( hCursor );
  168.               break;
  169.  
  170.       case WM_COMMAND :
  171.               switch( LOWORD( wParam ) )
  172.               {
  173.                  case IDM_TEST :
  174.                         {
  175.                             LPTSTR lpAND = GlobalLock( hmemAND );
  176.                             LPTSTR lpXOR = GlobalLock( hmemXOR );
  177.                          
  178.                             // Clear current cursor and delete it.
  179.                             //....................................
  180.                             SetCursor( NULL );
  181.                             DestroyCursor( hCursor );
  182.  
  183.                             // Add an X to the cursor bitmap.
  184.                             //...............................
  185.                             SelectObject( hDCBitmap,
  186.                                           GetStockObject( BLACK_PEN ) );
  187.                             MoveToEx( hDCBitmap, 0, 0, NULL );
  188.                             LineTo( hDCBitmap, nCursX, nCursY );
  189.                             MoveToEx( hDCBitmap, 0, nCursY, NULL );
  190.                             LineTo( hDCBitmap, nCursX, 0 );
  191.                             GetBitmapBits( hBM, (DWORD)nByteArea, lpAND );
  192.                        
  193.                             hCursor = CreateCursor( hInst, 0, 0, nCursX, 
  194.                                                     nCursY, lpAND, lpXOR );
  195.  
  196.                             SetCursor( hCursor );
  197.  
  198.                             GlobalUnlock( hmemAND );
  199.                             GlobalUnlock( hmemXOR );
  200.                         }
  201.                         break;
  202.  
  203.                  case IDM_ABOUT :
  204.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  205.                         break;
  206.  
  207.                  case IDM_EXIT :
  208.                         DestroyWindow( hWnd );
  209.                         break;
  210.               }
  211.               break;
  212.       
  213.       case WM_DESTROY :
  214.               DestroyCursor( hCursor );
  215.               DeleteObject( hBM );
  216.               DeleteDC( hDCBitmap );
  217.               GlobalFree( hmemAND );
  218.               GlobalFree( hmemXOR );  
  219.               PostQuitMessage(0);
  220.               break;
  221.  
  222.       default :
  223.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  224.    }
  225.  
  226.    return( 0L );
  227. }
  228.  
  229.  
  230. LRESULT CALLBACK About( HWND hDlg,           
  231.                         UINT message,        
  232.                         WPARAM wParam,       
  233.                         LPARAM lParam)
  234. {
  235.    switch (message) 
  236.    {
  237.        case WM_INITDIALOG: 
  238.                return (TRUE);
  239.  
  240.        case WM_COMMAND:                              
  241.                if (   LOWORD(wParam) == IDOK         
  242.                    || LOWORD(wParam) == IDCANCEL)    
  243.                {
  244.                        EndDialog(hDlg, TRUE);        
  245.                        return (TRUE);
  246.                }
  247.                break;
  248.    }
  249.  
  250.    return (FALSE); 
  251. }
  252.